import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:flutter_swiper_null_safety/src/transformer_page_view/transformer_page_view.dart';
import 'dart:math' as math;

class FlutterSwiperTest extends StatefulWidget {
  @override
  _FlutterSwiperTestState createState() => _FlutterSwiperTestState();
}

class _FlutterSwiperTestState extends State<FlutterSwiperTest> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 8),
      height: 200,
      child: Swiper(
        itemBuilder: (BuildContext context, int index) {
          return Image.network(
            'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=49081054,840221900&fm=224&gp=0.jpg',
            fit: BoxFit.cover,
          );
        },
        itemCount: 3,
        // autoplay: true,
        // autoplayDelay: 5000,
        // duration: 2000,
        pagination: SwiperPagination(
          builder: DotSwiperPaginationBuilder(
            size: 10,
            activeSize: 10,
            color: Color.fromRGBO(255, 255, 255, 0.8),
            activeColor: Colors.yellow,
          ),
        ),
        control: SwiperControl(
          color: Colors.red,
          padding: EdgeInsets.only(
            left: 7,
            right: 0,
          ),
        ),
        transformer: PageTransformerBuilder(
          builder: (Widget child, TransformInfo info) {
            /**
             * 不需要关注移动的方向，只需要记住 position 的变化即可，想象成金字塔，中间的就是金字塔的塔顶
            */
            double position = info.position;

            if (position < 0.0) {
              return new Transform.scale(
                scale: 1 + position * 0.3,
                child: child,
              );
            } else {
              return new Transform.scale(
                scale: 1 - position * 0.3,
                child: child,
              );
            }

            // return new Transform.rotate(
            //   angle: -position * math.pi / 4,
            //   // alignment: Alignment.topLeft,
            //   alignment: Alignment.topCenter,
            //   child: child,
            // );
          },
        ),
        viewportFraction: 0.7,
      ),
    );
  }
}
